package com.theo.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.theo.entity.CityInfo;
import com.theo.entity.NewsInfo;
import com.theo.entity.PatientInfo;
import com.theo.service.CityInfoService;
import com.theo.service.PatientInfoService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 方涛
 * @since 2021-02-02
 */
@Controller
@RequestMapping("/patient-info")
public class PatientInfoController {

    @Autowired
    private PatientInfoService patientInfoService;
    @Autowired
    private CityInfoService cityInfoService;
    @RequestMapping("/show.do")
    public String show(Model model, @RequestParam(value = "pn", defaultValue = "1") Integer pn) {
        PageHelper.startPage(pn, 8);
        List<PatientInfo> list = patientInfoService.queryAllPatientInfo();
        //list.forEach(System.out::println);
        PageInfo pageInfo = new PageInfo(list, 6);
        model.addAttribute("pageInfo", pageInfo);
        return "admin/patient_list";
    }

    @RequestMapping("/toInput.do")
    public String toInput(Model model) {
        List<CityInfo> provinces = cityInfoService.findProvinces();
        model.addAttribute("provinces", provinces);
        return "admin/patient_input";
    }

    @RequestMapping("/input.do")
    public String savePatient(PatientInfo patientInfo) {
        patientInfoService.savePatientInfo(patientInfo);
        return "redirect:/patient-info/show.do";
    }

    @RequestMapping("/delete.do")
    public String delete(Integer id) {
        patientInfoService.deletePatientInfo(id);
        return "redirect:/patient-info/show.do";
    }

    @RequestMapping("/update.do")
    public String update(PatientInfo patientInfo) {
        patientInfoService.updatePatientInfo(patientInfo);
        return "redirect:/patient-info/show.do";
    }

    @RequestMapping("/toUpdate.do")
    public String toUpdate(Model model, Integer id) {
        PatientInfo patientInfo = patientInfoService.queryPatientInfoById(id);
        System.out.println(patientInfo);
        model.addAttribute("patient", patientInfo);
        List<CityInfo> provinces = cityInfoService.findProvinces();
        provinces.forEach(System.out::println);
        model.addAttribute("provinces", provinces);
        return "admin/patient_update";
    }
}

